package zcw.com.lib_leet_code;

import zcw.com.lib_leet_code.bean.ListNode;
import zcw.com.lib_leet_code.util.Util;

/**
 * Created by 朱城委 on 2021/7/12.<br><br>
 *
 * 移除链表元素
 */
public class Topic203 {
    public static void main(String[] args) {
        Topic203 instance = new Topic203();

        int[] array = new int[] {1,2,6,3,4,5,6};
        ListNode head = Util.createList(array);
        Util.printList(instance.removeElements(head, 6));
    }

    public ListNode removeElements(ListNode head, int val) {
        while (head != null && head.val == val) {
            head = head.next;
        }

        if(head == null) {
            return null;
        }

        ListNode node = head;
        while (node.next != null) {
            if(node.next.val == val) {
                node.next = node.next.next;
            }
            else {
                node = node.next;
            }
        }

        return head;
    }
}
